
var toltal = 59;
var listType = "sale";
var curPage = 1;
var type = 0;
var temp = 0;
var ph = 0;

$(document).ready(function() {
	getTotal(curPage);
	getPage(curPage);
});

function getType(slt) {
	type = slt;
	$("#mu a").each(function() {
		if ($(this).attr("type") == type) {
			$(this).addClass("now1");
		} else {
			$(this).removeClass("now1");
		}
	});

	$(".tag a").each(function() {
		$(this).removeClass("now2");
	});
	getTotal(curPage);

	getPage(curPage);
}

function getTemp(slt) {
	temp = slt;
	$(".tag a").each(function() {
		if ($(this).attr("temp") == temp) {
			$(this).addClass("now2");
		} else {
			$(this).removeClass("now2");
		}
	});
	getPage(curPage);
}

function getTotal(curPage) {
	$.ajax({
		type : "post",
		async : false,
		url : "getGoods.action",
		dataType : "json",
		data : {
			"listTypes" : listType,
			"types" : type,
			"temps" : temp,
			"curPages" : curPage,
			"toltal" : 0
		},
		success : function(result) {
			toltal = result;
		}
	});
}

function getLength() {
	var height = $('#here').outerHeight(true);
	var Y = $('#here').position().top;
	ph = height + Y - 50;
}

function getData(curPage) {
	$.ajax({
		type : "post",
		async : false,
		url : "getGoods.action",
		dataType : "json",
		data : {
			"listTypes" : listType,
			"types" : type,
			"temps" : temp,
			"curPages" : curPage,
			"toltal" : 1
		},
		success : getGoods
	});
}

var content = "";
function getGoods(data) {
	alert(data);
	content = data;
	var a ="aa";
	var outputb = "";
	for ( var i = 0; i < data.length; i++) {
		var name = data[i].uid;
		outputb += '<div class="box">';
		outputb += '<table border="0"><tr><td style="width: 40%;text-align: left">';
		outputb += '<a onclick="javascript:showper(\''+name+'\')"><img src="' + data[i].topImage + '"/></a></td>';
		outputb += '<td style="padding-top: 20px;vertical-align: top;font-weight:bold">';
		outputb += '<a href="#">' + data[i].userName + '</a><br> <br>';
		outputb += '<span style="font-size: 12px;color:gray">'
				+ data[i].createDate + '</span></td></tr>';
		outputb += '<tr><td colspan="2" style="width: 120px;text-align:left;padding-left:20px">';
		outputb += '<a href="detail.action?goodId=' + data[i].goodId + '">'
				+ data[i].goodName + '</a><br /> <br />数量 :' + data[i].count
				+ '<br />';
		outputb += '<span style="font-weight:bold;color:crimson">估价 :'
				+ data[i].price + 'RMB</span></td>';
		outputb += '</tr></table>';
		outputb += '</div>';
	}
	document.getElementById("here").innerHTML = outputb;
	getLength();
}

function getPage(curPage) {
	getData(curPage);
	supage(curPage, toltal);
}

function supage(curPage, total) {
	var output = '<div class="pagination" style="top:' + ph + 'px">';
	var pageSize = 16;
	if (parseInt(total) == 0 || parseInt(total) == 'NaN')
		return;
	var totalPage = Math.ceil(total / pageSize);
	var curPage = parseInt(curPage) > 0 ? parseInt(curPage) : 1;

	if (totalPage > 5) {
		if ((curPage - 2) > 0 && curPage < totalPage - 2) {
			var start = curPage - 2;
			var end = curPage + 2;
		} else if (curPage >= (totalPage - 2)) {
			var start = totalPage - 2;
			var end = totalPage;
		} else {
			var start = 1;
			var end = 5;
		}
	} else {
		var start = 1;
		var end = totalPage;
	}

	//首页控制   
	if (curPage > 1) {
		output += '<a href="javascript:getPage(1);"   title="第一页" class="page-first">首页</a>';
	} else {
		output += '<span class="page-disabled">首页</span> ';
	}
	//上一页菜单控制   
	if (curPage > 1) {
		output += '<a href="javascript:getPage(' + (curPage - 1)
				+ ');"  title="上一页" class="page-previous">上一页</a>';
	} else {
		output += '<span class="page-disabled">上一页</span>';
	}

	//页码展示   
	for (i = start; i <= end; i++) {
		if (i == curPage) {
			output += '<a href="javascript:;" class="page-cur">' + curPage
					+ '</a>';
		} else {
			output += '<a href="javascript:getPage(' + i + ');">' + i + '</a>';
		}
	}
	//下一页菜单控制   
	if (totalPage > 1 && curPage < totalPage) {
		output += '<a title="下一页" href="javascript:getPage(' + (curPage + 1)
				+ ');"  class="page-next">下一页</a>';
	} else {
		output += '<span class="page-disabled">下一页</span>';
	}
	//最后页控制   
	if (curPage < totalPage) {
		output += '<a title="最后页"   href="javascript:getPage(' + totalPage
				+ ');" class="page-end">尾页</a>';
	} else {
		output += '<span class="page-disabled">尾页</span>';
	}

	output += '</div>';

	document.getElementById("pageNav").innerHTML = output;
};

function showper(uid) {
	for ( var i = 0; i < content.length; i++) {
		if (uid == content[i].uid) {
			$("#img").text("图片");
			$("#name").text(content[i].userName);
			$("#pv").text(content[i].pv);
			$("#sign").text(content[i].sign);
		}
	}
	$(".per").fadeIn("slow");
}

function closeper() {
	$(".per").fadeOut("slow");
}
